cd ~

, with read , about 11min.

Vue深入组件 - 禁用Attributes继承

attribute 和 property 的区别

property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴。

简单理解,Attribute就是dom节点自带的属性,例如html中常用的id、class、title、align等。

而Property是这个DOM元素作为对象,其附加的内容,例如childNodes、firstChild等。

@cnblog:lmjZone

有这样一个嵌套组件,父组件为:

<template>
  <div class="father-wrapper">
    <h1>father component</h1>
    <ChildComp/>
  </div>
</template>
copy success

子组件为:

<template>
  <div class="child-wrapper">
    <h1>child component</h1>
    <div class="child-div">
      这是子组件中的div block
    </div>
  </div>
</template>
copy success

为了看起来直观,加了少许样式,现在看起来就像这样:

image-20210823231835564

如果,我们在<ChildComp /> 上加上一个Attribute ,如 :

 <ChildComp attribute1="hello"/>
copy success

会发现:

image-20210823233406453

我们将attribute添加在了 div.child-wrapper 上, 这实际上符合我们的预期的。

但是究其原因,官方是这样解释的:

@vue/doc:非-Prop-的-Attribute

意思就是说,在父组件中,如果你给一个子组件传递一个attribute(注意,这叫做非prop的Attribute),那么attribute会被解析为html 属性作用到根元素,即div.child-wrapper , 它的表现就像是继承一样。

但是,有一个问题,如果某些场景下,你期望不让它默认的作用在根元素上 div.child-wrapper , 那该怎么办?
例如,我们需要加在 div.child-div上?

这是时候,我们需要做的就是:

  1. 禁用掉这样一种默认的作用规则;
  2. 在子组件中取得attribute变量,然后绑定在 div.child-div

实际上操作起来很简单:

  1. 禁止继承(禁止默认的作用规则):

    export default {
      inheritAttrs: false,
        ...
    copy success
  2. 取得attribute 变量,绑定到指定元素上(自定义我们需要的作用规则):
    在vue中,可以通过this.$attrs 获取到所有的 非prop的Attributes

    <template>
      <div class="child-wrapper">
        <h1>child component</h1>
        <div class="child-div" v-bind="$attrs"> <!-- <--这里 -->
          这是子组件中的div block
        </div>
      </div>
    </template>
    copy success

    image-20210823234112901

这就是 禁用 Attribute 继承

cd ~
GO BACK (Backspace)
BACK TO TOP (ESC)
COMMENTS (C)